<?php
namespace Home\Controller;

class CatController extends BaseController {
    function showlist(){
        $cat = D('Cat');
       if($_GET['c_id']){
           $cat ->delete($_GET['c_id']);
       }
        $user_id = session('user_id');
        $info = $cat->where("c_user_id = $user_id and c_status = 0")->select();
        $price = $cat->field('sum(c_price)')->where("c_user_id = $user_id and c_status = 0")->find();
        if(!empty($info)){
        $this->assign('info',$info);
        $this->assign('price',$price);
        $this->display();
        } else {
            $this->redirect('Goods/showlist',array(),2,'购物车空空如也,先去购物吧!');
        }
    }
    function tianjia(){
        if(!empty($_POST)){
            $goods = D('Goods')->find($_POST['goods_id']);
            $cat = D('Cat');
            $cat->c_goods_id = $goods['goods_id'];
            $cat->c_goods_number = $_POST['goods_number'];
            $cat->c_goods_name = $goods['goods_name'];
            $cat->c_price = $goods['goods_price']*$_POST['goods_number'];            
            $cat->c_goods_price = $goods['goods_price'];
            $cat->c_user_id = session('user_id');
            $cat->c_goods_img = $goods['goods_small_img'];
            $z = $cat->add();
            if($z){
                $this->redirect('Goods/showlist',array(),2,'添加购物车成功!');
            }else{
                $this->redirect('Goods/detail',array(),2,'添加购物车失败!');
            }
        }else{
           $this->redirect('Goods/showlist');
        }
    }
   function jiesuan(){
        $cat = D('Cat');
        $user_id = session('user_id');
        $info = $cat->where("c_user_id = $user_id and c_status=0")->select();
        $price = $cat->field('sum(c_price)')->where("c_user_id = $user_id and c_status=0")->find();
        $this->assign('info',$info);
        $this->assign('price',$price);
        $this->display();
   }
}